
from langchain.schema.messages import SystemMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

# 初始化 LLM
model = ChatOpenAI(
    openai_api_key="key",
    openai_api_base="https://api.moonshot.cn/v1",
    model="moonshot-v1-8k",
    temperature=0,
    request_timeout=60,
    max_retries=3,
)

# 创建消息列表
messages = [
    SystemMessage(content="把下面的语句翻译为英文。"),
    HumanMessage(content="今天天气怎么样？"),
]

# # 调用 LLM 并传入消息
# response = llm(messages)
#
# # 输出响应
# print(response.content)
# print(model.invoke(messages).content)
#
# result=model.invoke(messages)
parser=StrOutputParser()
# print(parser.invoke(result))

# model.invoke(messages).pretty_print()

template=ChatPromptTemplate.from_messages([
    ("system","把下面的内容翻译为{language}"),
    ("user","{text}")
])
# prompt=template.invoke({"language":"日语","text":"今天天气如何?"})

chain=template|model|parser
print(chain.invoke({"language":"日语","text":"今天天气如何?"}))